<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>children 聚合 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'search-aggregations-bucket-children-aggregation.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-bucket-children-aggregation.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-bucket-children-aggregation.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/search-aggregations-bucket-children-aggregation.html" rel="nofollow" target="_blank">../en/search-aggregations-bucket-children-aggregation.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations-bucket.html">桶(bucket)聚合</a></span>
»
<span class="breadcrumb-node">children 聚合</span>
</div>
<div class="navheader">
<span class="prev">
<a href="search-aggregations-bucket-autodatehistogram-aggregation.html">« auto_date_histogram 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-bucket-composite-aggregation.html">composite 聚合 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="search-aggregations-bucket-children-aggregation"></a>children 聚合
</h2>
</div></div></div>
<p>
一种特殊的单桶聚合，它选择具有指定类型的子文档，如 <a class="xref" href="parent-join.html" title="join 数据类型" rel="nofollow"><code class="literal">join</code>字段</a>中定义的那样。
</p>
<p>这个聚合只有一个选项：</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
<code class="literal">type</code> - 应该选择的子类型。
</li>
</ul>
</div>
<p>例如，假设我们有一个问答索引。答案的类型在映射中具有以下 <code class="literal">join</code> 字段：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT child_example
{
  "mappings": {
    "properties": {
      "join": {
        "type": "join",
        "relations": {
          "question": "answer"
        }
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/419.console"></div>
<p>
<code class="literal">question</code> 文档包含 tag 字段，<code class="literal">answer</code> 文档包含 owner 字段。

使用 <code class="literal">children</code> 聚合，即使两个字段存在于两种不同类型的文档中，也可以在单个请求中将 tag 桶映射到 owner 桶。
</p>
<p><code class="literal">question</code>文档的示例：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT child_example/_doc/1
{
  "join": {
    "name": "question"
  },
  "body": "&lt;p&gt;I have Windows 2003 server and i bought a new Windows 2008 server...",
  "title": "Whats the best way to file transfer my site from server to a newer one?",
  "tags": [
    "windows-server-2003",
    "windows-server-2008",
    "file-transfer"
  ]
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/420.console"></div>
<p><code class="literal">answer</code>文档的示例：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT child_example/_doc/2?routing=1
{
  "join": {
    "name": "answer",
    "parent": "1"
  },
  "owner": {
    "location": "Norfolk, United Kingdom",
    "display_name": "Sam",
    "id": 48
  },
  "body": "&lt;p&gt;Unfortunately you're pretty much limited to FTP...",
  "creation_date": "2009-05-04T13:45:37.030"
}

PUT child_example/_doc/3?routing=1&amp;refresh
{
  "join": {
    "name": "answer",
    "parent": "1"
  },
  "owner": {
    "location": "Norfolk, United Kingdom",
    "display_name": "Troll",
    "id": 49
  },
  "body": "&lt;p&gt;Use Linux...",
  "creation_date": "2009-05-05T13:45:37.030"
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/421.console"></div>
<p>可以构建以下请求，将两者连接在一起：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST child_example/_search?size=0
{
  "aggs": {
    "top-tags": {
      "terms": {
        "field": "tags.keyword",
        "size": 10
      },
      "aggs": {
        "to-answers": {
          "children": {
            "type" : "answer" <a id="CO213-1"></a><i class="conum" data-value="1"></i>
          },
          "aggs": {
            "top-names": {
              "terms": {
                "field": "owner.display_name.keyword",
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/422.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO213-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">type</code> 指向名为<code class="literal">answer</code>的类型/映射。</p>
</td>
</tr>
</table>
</div>
<p>上面的例子返回前几个问题标签和每个标签前几个答案的所有者。</p>
<p>响应可能是：</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
  "took": 25,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped" : 0,
    "failed": 0
  },
  "hits": {
    "total" : {
      "value": 3,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "top-tags": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "file-transfer",
          "doc_count": 1, <a id="CO214-1"></a><i class="conum" data-value="1"></i>
          "to-answers": {
            "doc_count": 2, <a id="CO214-2"></a><i class="conum" data-value="2"></i>
            "top-names": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": "Sam",
                  "doc_count": 1
                },
                {
                  "key": "Troll",
                  "doc_count": 1
                }
              ]
            }
          }
        },
        {
          "key": "windows-server-2003",
          "doc_count": 1, <a id="CO214-3"></a><i class="conum" data-value="1"></i>
          "to-answers": {
            "doc_count": 2, <a id="CO214-4"></a><i class="conum" data-value="2"></i>
            "top-names": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": "Sam",
                  "doc_count": 1
                },
                {
                  "key": "Troll",
                  "doc_count": 1
                }
              ]
            }
          }
        },
        {
          "key": "windows-server-2008",
          "doc_count": 1, <a id="CO214-5"></a><i class="conum" data-value="1"></i>
          "to-answers": {
            "doc_count": 2, <a id="CO214-6"></a><i class="conum" data-value="2"></i>
            "top-names": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": "Sam",
                  "doc_count": 1
                },
                {
                  "key": "Troll",
                  "doc_count": 1
                }
              ]
            }
          }
        }
      ]
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO214-1"><i class="conum" data-value="1"></i></a><a href="#CO214-3"></a><a href="#CO214-5"></a></p>
</td>
<td align="left" valign="top">
<p>标签为<code class="literal">file-transfer</code>、<code class="literal">windows-server-2003</code>等的 question 文档的数量。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO214-2"><i class="conum" data-value="2"></i></a><a href="#CO214-4"></a><a href="#CO214-6"></a></p>
</td>
<td align="left" valign="top">
<p>与带有标签<code class="literal">file-transfer</code>、<code class="literal">windows-server-2003</code>等的 question 文档相关的 answer 文档的数量。</p>
</td>
</tr>
</table>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="search-aggregations-bucket-autodatehistogram-aggregation.html">« auto_date_histogram 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-bucket-composite-aggregation.html">composite 聚合 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>